﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace BenhVien
{
    public partial class fmAddStaff : Form
    {
        private DataTable dt = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();
        private Connection cons= new Connection();
        public fmAddStaff()
        {
            InitializeComponent();
        }

        private void fmAddStaff_Load(object sender, EventArgs e)
        {
            this.cbChucVu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbGioiTinh.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbHinhThuc.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbHopDong.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            getData();
        }

        private void getData()
        {
            cons.Connect();
            SqlCommand command = new SqlCommand();
            command.Connection = cons.con;
            command.CommandType = CommandType.Text;
            command.CommandText = "Select * from NhanVien";
            da.SelectCommand = command;
            da.Fill(dt);
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            dt.Clear();
            try
            {
                if (!checkMaNV(tbMaNV.Text))
                {
                    DataRow row = dt.NewRow();

                    if (tbMaNV.Text != "")
                    {
                        row["MaNV"] = tbMaNV.Text;
                        row["Ho"] = tbHo.Text;
                        if (tbTen.Text != "")
                        {
                            row["Ten"] = tbTen.Text;
                            row["DiaChi"] = tbDiaChi.Text;
                            if (tbSdt.Text == "") row["SoDienThoai"] = 0;
                            else row["SoDienThoai"] = tbSdt.Text;
                            row["NgaySinh"] = tbNgaySinh.Text;
                            if (cbGioiTinh.Text == "Nam") row["GioiTinh"] = 1;
                            else if(cbGioiTinh.Text=="Nữ")row["GioiTinh"] = 0;
                            if (tbSCM.Text.Length == 9)
                            {
                                row["MaBaoHiem"] = tbSCM.Text;
                                if (cbChucVu.Text!="GD NV"&&cbChucVu.Text!="GD BV")
                                {
                                    row["Vitri"] = cbChucVu.Text;
                                    row["Luong"] = tbLuong.Text;
                                    row["BacLuong"] = tbBacLuong.Text;
                                    if (Convert.ToInt32(tbMaPhong.Text) > 0 && Convert.ToInt32(tbMaPhong.Text) < 18)
                                    {
                                        row["MaPhong"] = tbMaPhong.Text;
                                        if (Convert.ToInt32(tbGioLam.Text) <= 56)
                                        {
                                            row["SoGioLamTrongTuan"] = tbGioLam.Text;
                                            row["HinhThucTraLuong"] = cbHinhThuc.Text;
                                            row["HopDong"] = cbHopDong.Text;
                                            dt.Rows.Add(row);

                                            // Insert
                                            SqlCommand commandInsert = new SqlCommand();
                                            commandInsert.Connection = cons.con;
                                            commandInsert.CommandType = CommandType.Text;
                                            commandInsert.CommandText = @"Insert NhanVien(MaNV, Ho, Ten, DiaChi, SoDienThoai, NgaySinh, GioiTinh, MaBaoHiem, Vitri, Luong, BacLuong, MaPhong, SoGioLamTrongTuan, HinhThucTraLuong, HopDong)
                                            Values(@MaNV, @Ho, @Ten, @DiaChi, @SoDienThoai, @NgaySinh, @GioiTinh, @MaBaoHiem, @Vitri, @Luong, @BacLuong, @MaPhong, @SoGioLamTrongTuan, @HinhThucTraLuong, @HopDong)";

                                            commandInsert.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                                            commandInsert.Parameters.Add("@Ho", SqlDbType.NVarChar, 50, "Ho");
                                            commandInsert.Parameters.Add("@Ten", SqlDbType.NVarChar, 50, "Ten");
                                            commandInsert.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 50, "DiaChi");
                                            commandInsert.Parameters.Add("@SoDienThoai", SqlDbType.NVarChar, 50, "SoDienThoai");
                                            commandInsert.Parameters.Add("@NgaySinh", SqlDbType.DateTime, 15, "NgaySinh");
                                            commandInsert.Parameters.Add("@GioiTinh", SqlDbType.Int, 50, "GioiTinh");
                                            commandInsert.Parameters.Add("@MaBaoHiem", SqlDbType.NVarChar, 50, "MaBaoHiem");
                                            commandInsert.Parameters.Add("@Vitri", SqlDbType.NVarChar, 50, "Vitri");
                                            commandInsert.Parameters.Add("@Luong", SqlDbType.Float, 50, "Luong");
                                            commandInsert.Parameters.Add("@BacLuong", SqlDbType.Float, 50, "BacLuong");
                                            commandInsert.Parameters.Add("@MaPhong", SqlDbType.NVarChar, 50, "MaPhong");
                                            commandInsert.Parameters.Add("@SoGioLamTrongTuan", SqlDbType.Int, 3, "SoGioLamTrongTuan");
                                            commandInsert.Parameters.Add("@HinhThucTraLuong", SqlDbType.NVarChar, 50, "HinhThucTraLuong");
                                            commandInsert.Parameters.Add("@HopDong", SqlDbType.NVarChar, 50, "HopDong");
                                            da.InsertCommand = commandInsert;
                                            da.Update(dt);
                                            MessageBox.Show("Add will success", "Message", MessageBoxButtons.OK);
                                            //btDefaut_Click(null, null);
                                        }
                                        else
                                        {
                                            if (MessageBox.Show("Số giờ làm đã bị sai", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                            {
                                                this.Close();
                                                frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                                _fmManagerStaff.Show();
                                            }
                                        }

                                    }
                                    else
                                    {
                                        if (MessageBox.Show("Bệnh viện chỉ có tất cả 17 phòng", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                        {
                                            this.Close();
                                            frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                            _fmManagerStaff.Show();
                                        }
                                    }
                                }
                                else
                                {
                                    if (!checkStaff(cbChucVu.Text))
                                    {
                                        row["Vitri"] = cbChucVu.Text;
                                        row["Luong"] = tbLuong.Text;
                                        row["BacLuong"] = tbBacLuong.Text;
                                        if (Convert.ToInt32(tbMaPhong.Text) > 0 && Convert.ToInt32(tbMaPhong.Text) < 18)
                                        {
                                            row["MaPhong"] = tbMaPhong.Text;
                                            if (Convert.ToInt32(tbGioLam.Text) <= 56)
                                            {
                                                row["SoGioLamTrongTuan"] = tbGioLam.Text;
                                                row["HinhThucTraLuong"] = cbHinhThuc.Text;
                                                row["HopDong"] = cbHopDong.Text;
                                                dt.Rows.Add(row);

                                                // Insert
                                                SqlCommand commandInsert = new SqlCommand();
                                                commandInsert.Connection = cons.con;
                                                commandInsert.CommandType = CommandType.Text;
                                                commandInsert.CommandText = @"Insert NhanVien(MaNV, Ho, Ten, DiaChi, SoDienThoai, NgaySinh, GioiTinh, MaBaoHiem, Vitri, Luong, BacLuong, MaPhong, SoGioLamTrongTuan, HinhThucTraLuong, HopDong)
                                            Values(@MaNV, @Ho, @Ten, @DiaChi, @SoDienThoai, @NgaySinh, @GioiTinh, @MaBaoHiem, @Vitri, @Luong, @BacLuong, @MaPhong, @SoGioLamTrongTuan, @HinhThucTraLuong, @HopDong)";

                                                commandInsert.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                                                commandInsert.Parameters.Add("@Ho", SqlDbType.NChar, 10, "Ho");
                                                commandInsert.Parameters.Add("@Ten", SqlDbType.NVarChar, 50, "Ten");
                                                commandInsert.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 50, "DiaChi");
                                                commandInsert.Parameters.Add("@SoDienThoai", SqlDbType.NVarChar, 50, "SoDienThoai");
                                                commandInsert.Parameters.Add("@NgaySinh", SqlDbType.DateTime, 15, "NgaySinh");
                                                commandInsert.Parameters.Add("@GioiTinh", SqlDbType.Int, 50, "GioiTinh");
                                                commandInsert.Parameters.Add("@MaBaoHiem", SqlDbType.NVarChar, 50, "MaBaoHiem");
                                                commandInsert.Parameters.Add("@Vitri", SqlDbType.NVarChar, 50, "Vitri");
                                                commandInsert.Parameters.Add("@Luong", SqlDbType.Float, 50, "Luong");
                                                commandInsert.Parameters.Add("@BacLuong", SqlDbType.Float, 50, "BacLuong");
                                                commandInsert.Parameters.Add("@MaPhong", SqlDbType.NVarChar, 50, "MaPhong");
                                                commandInsert.Parameters.Add("@SoGioLamTrongTuan", SqlDbType.Int, 3, "SoGioLamTrongTuan");
                                                commandInsert.Parameters.Add("@HinhThucTraLuong", SqlDbType.NVarChar, 10, "HinhThucTraLuong");
                                                commandInsert.Parameters.Add("@HopDong", SqlDbType.NVarChar, 10, "HopDong");
                                                da.InsertCommand = commandInsert;
                                                da.Update(dt);
                                                MessageBox.Show("Add will success", "Message", MessageBoxButtons.OK);
                                                //btDefaut_Click(null, null);
                                            }
                                            else
                                            {
                                                if (MessageBox.Show("Số giờ làm đã bị sai", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                                {
                                                    this.Close();
                                                    frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                                    _fmManagerStaff.Show();
                                                }
                                            }

                                        }
                                        else
                                        {
                                            if (MessageBox.Show("Bệnh viện chỉ có tất cả 17 phòng", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                            {
                                                this.Close();
                                                frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                                _fmManagerStaff.Show();
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (MessageBox.Show("Giám đốc bênh viện hoặc giám đốc nhân viên chỉ có một người", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                        {
                                            this.Close();
                                            frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                            _fmManagerStaff.Show();
                                        }
                                    }
                                    
                                }
                                
                            }
                            else
                            {
                                if (MessageBox.Show("Số chứng minh phải có 9 số", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                {
                                    this.Close();
                                    frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                    _fmManagerStaff.Show();
                                }
                            }
                            if (cbGioiTinh.Text == "Nam") row["GioiTinh"] = 1;
                            else { if (cbGioiTinh.Text == "Nữ") row["GioiTinh"] = 0; }


                        }
                        else
                        {
                            if (MessageBox.Show("Bạn phải nhập Tên Nhân Viên.", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            {
                                this.Close();
                                frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                _fmManagerStaff.Show();
                            }
                            this.ActiveControl = tbTen;
                        }

                    }
                    else
                    {
                        if (MessageBox.Show("Bạn phải nhập Mã Nhân Viên", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            this.Close();
                            frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                            _fmManagerStaff.Show();
                        }
                        this.ActiveControl = tbMaNV;
                    }

                }
                else
                {
                    if (MessageBox.Show("Ma nhan vien bi trung", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        this.Close();
                        frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                        _fmManagerStaff.Show();
                    }
                }
            }
            catch(Exception err)
            {
                MessageBox.Show("Ngày sinh sai!"+err.ToString());
            }
            
            //cons.Connect();
            
            /*this.Close();
            frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
            _fmManagerStaff.Show();*/

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
            frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
            _fmManagerStaff.Show();
        }

        private void btDefaut_Click(object sender, EventArgs e)
        {
            tbMaNV.Text = "";
            tbHo.Text = "";
            tbTen.Text = "";
            tbDiaChi.Text = "";
            tbSdt.Text = "";
            tbSCM.Text = "";
            tbLuong.Text = "";
            tbBacLuong.Text = "";
            tbGioLam.Text = "";
            tbNgaySinh.Text = "";
            tbMaPhong.Text = "";
            cbChucVu.Text = "Y Tá";
            cbGioiTinh.Text = "Nam";
            cbHinhThuc.Text = "Tháng";
            cbHopDong.Text = "Có";
        }

        private bool checkMaNV(String s)
        {
            Boolean check = false;
            DataTable dtCheck = new DataTable();
            SqlCommand commmandCheck = new SqlCommand();
            commmandCheck.Connection = cons.con;
            commmandCheck.CommandType = CommandType.Text;
            commmandCheck.CommandText = "Select MaNV from NhanVien Where MaNV=\'"+s+"\'";
            da.SelectCommand = commmandCheck;
            da.Fill(dtCheck);
            if (dtCheck.Rows.Count > 0) check = true;
            return check;
        }
        private bool checkStaff(String cv)
        {
            Boolean checkstaff = false;
            
            DataTable dtcheckGDBV = new DataTable();
            DataTable dtcheckGDNV = new DataTable();
            SqlCommand commandcheckstaff = new SqlCommand();
            commandcheckstaff.Connection = cons.con;
            commandcheckstaff.CommandType = CommandType.Text;
            commandcheckstaff.CommandText = "Select Vitri from NhanVien Where Vitri= 'GD BV'";
            da.SelectCommand = commandcheckstaff;
            da.Fill(dtcheckGDBV);
           
            commandcheckstaff.CommandText = "Select Vitri from NhanVien Where Vitri= 'GD NV'";
            da.SelectCommand = commandcheckstaff;
            da.Fill(dtcheckGDNV);
            if (dtcheckGDBV.Rows.Count > 0 && cv == "GD BV") checkstaff = true;
            if (dtcheckGDNV.Rows.Count > 0 && cv == "GD NV") checkstaff = true;
           
            return checkstaff;
        }
    }
}
